#!/usr/bin/env bash
#
# Script to generate documentation for Rust crates to publish as gh-pages.
#
# Usage:
#
# ./scripts/build_gh_pages <target_dir>
#
# Where <target_dir> must be an empty directory.
#
# shellcheck disable=SC2115

readonly SCRIPTS_DIR="$(dirname "$0")"
# shellcheck source=scripts/common
source "$SCRIPTS_DIR/common"

readonly TARGET_DIR="${1:-}"
readonly DEFAULT_TARGET=x86_64-unknown-linux-gnu

if [[ -z "${TARGET_DIR}" ]]; then
  echo 'target dir not specified'
  exit 1
fi

if [[ ! -d "${TARGET_DIR}" ]]; then
  echo 'target not a directory'
  exit 1
fi

if [[ -n "$(ls "${TARGET_DIR}"/*)" ]]; then
  echo 'target dir not empty'
  exit 1
fi

RUSTDOCFLAGS="--enable-index-page -Zunstable-options" \
    cargo doc --no-deps --target-dir="${TARGET_DIR}"

# Remove non-doc artifacts from the target dir.
rm --recursive --force "${TARGET_DIR}/debug"
rm --recursive --force "${TARGET_DIR}/${DEFAULT_TARGET}/debug"

# Remove non-deterministic files.
rm --force "${TARGET_DIR}/.rustc_info.json"

# Remove unnecessary lock file, which is also owned by root and therefore problematic.
rm --force "${TARGET_DIR}/${DEFAULT_TARGET}/doc/.lock"

# Strip the target triple from the path.
mv "${TARGET_DIR}/${DEFAULT_TARGET}/doc" "${TARGET_DIR}/doc"

# Remove the target triple directory.
rm --recursive --force "${TARGET_DIR}/${DEFAULT_TARGET}"

# Create a top-level index.html which redirects to the rustdoc index.
cat <<-END > "${TARGET_DIR}/index.html"
  <!DOCTYPE html>
  <html>
    <head>
      <meta http-equiv="Refresh" content="0; url=doc/doc/index.html"/>
    </head>
    <body>
      <p><a href="doc/doc/index.html">doc/doc/index.html</a></p>
    </body>
  </html>
END
